/**
 * 55. 跳跃游戏
 * https://leetcode-cn.com/problems/jump-game/
 */

/**
 * @param {number[]} nums
 * @return {boolean}
 */
function canJump(nums) {
  let max = 0;
  const last = nums.length - 1;
  for (let i = 0; i < last; i += 1) {
    if (i > max) {
      return false;
    }
    max = Math.max(max, i + nums[i]);
  }
  return last <= max;
}

console.log(canJump([2, 3, 1, 1, 4]) === true);
console.log(canJump([3, 2, 1, 0, 4]) === false);
